package com.itheima.tcp4;

import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class ServerReaderRunnable implements Runnable {

    private Socket socket;

    public ServerReaderRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            //给当前对应的浏览器管道响应一个网页数据
            //通过字节输出流包装写数据给浏览器
            OutputStream os = socket.getOutputStream();

            //把字节输出流包装成打印流
            PrintStream ps = new PrintStream(os);

            //写响应的网页数据出去
            ps.println("HTTP/1.1 200 OK");
            ps.println("Content-Type:text/html;charset=utf-8");
            ps.println();       //必须换一行
            ps.println("<html>");
            ps.println("<head>");
            ps.println("<meta charset='utf-8'/>");
            ps.println("<title>");
            ps.println("Java+AI课程");
            ps.println("</title>");
            ps.println("<body>");
            ps.println("<h1 style='color:red;font-size=20px'>欢迎访问黑马程序员Java课程！</h1>");
            //响应一个黑马程序员官网的logo图片展示
            ps.println("<img src='https://www.itheima.com/images/logo.png'/>");
            ps.println("</body>");
            ps.println("</html>");

            ps.close();
            socket.close();

        } catch (Exception e) {
            //e.printStackTrace();

            //下线
            System.out.println("1个客户端连接断开！"+socket.getInetAddress().getHostAddress());
        }


    }
}
